package com.daocao.ds.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.daocao.ds.R;
import com.daocao.ds.api.Api;
import com.daocao.ds.api.bean.RuleSaveBean;
import com.daocao.ds.base.BaseActivity;
import com.daocao.ds.constant.UserInfoGlobal;
import com.daocao.ds.event.QuaSetSuccessEvent;
import com.daocao.ds.event.SetPayPassSuccessEvent;
import com.daocao.ds.util.MD5;
import com.daocao.ds.util.Network;
import com.daocao.ds.util.RxLifeUtil;
import com.daocao.ds.util.ToastUtil;
import com.daocao.ds.util.UI;
import com.daocao.ds.view.PayMoneyDilaog;
import com.daocao.ds.view.SelectMenuTipsDilaog;
import com.daocao.ds.view.V19FrameLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class QuataSettingActivity extends BaseActivity {

    @BindView(R.id.txt_title)
    TextView txtTitle;

    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.edit_money)
    EditText editMoney;
    @BindView(R.id.txt_my_balance)
    TextView txtMyBalance;
    @BindView(R.id.btn_submit)
    SuperButton btnSubmit;
    @BindView(R.id.txt_small_horn)
    TextView txtSmallHorn;
    @BindView(R.id.txt_notice)
    TextView txtNotice;

    private Unbinder unbiner;
    private SelectMenuTipsDilaog selectMenuTipsDilaog;
    private RuleSaveBean submitBean;
    private PayMoneyDilaog payMoneyDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quata_setting);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        submitBean = getIntent().getParcelableExtra("data");
        String small_horn = getIntent().getStringExtra("small_horn");
        txtSmallHorn.setText(small_horn);
        ArrayList<String> notice = getIntent().getParcelableExtra("notice");
        if (notice != null) {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < notice.size(); i++) {
                builder.append(notice.get(i) + "\n");
            }
            txtNotice.setText(builder.toString());
        }


        txtTitle.setText("设置每日上限额度");
        selectMenuTipsDilaog = new SelectMenuTipsDilaog(this);
        selectMenuTipsDilaog.showOneSelectData("老板，恭喜您", R.mipmap.img_apply_success, "成功开启自动抢单模式，并成功开通了免额支付放款功能", "我要抢单");
        selectMenuTipsDilaog.setOnChooseListener(new SelectMenuTipsDilaog.OnChooseListener() {
            @Override
            public void onLeft() {
                finish();
            }

            @Override
            public void onRight() {

            }
        });
        payMoneyDialog = new PayMoneyDilaog(this);
        payMoneyDialog.setOnChooseListener(new PayMoneyDilaog.OnChooseListener() {
            @Override
            public void onSuccess(String pass) {
                submitBean.pay_passwd = MD5.getMD5str(pass);
                getRulesSave();
            }
        });
    }

    public void getRulesSave() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        loadingDialog.show();
        Api.api_service.getRulesSave(submitBean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    UserInfoGlobal.saveAutoRule("ON");
                    EventBus.getDefault().post(new QuaSetSuccessEvent());
                    payMoneyDialog.dismiss();
                    selectMenuTipsDilaog.show();
                });
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_back, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_submit:
                if (UI.toInteger(editMoney) % 100 == 0) {
                    submitBean.day_max_amount = UI.toString(editMoney);
                    payMoneyDialog.show();
                } else {
                    ToastUtil.makeText(this, "请输入100的整数");
                }

                break;
        }
    }
}
